package com.xl.flinkdemo.聚合算子;

import com.xl.flinkdemo.entity.SensorReading;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * 基于流处理操作的API
 **/
public class ReduceTest {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 从文件中读取数据
        env.setParallelism(1);
        String inputPath = "D:\\myWork\\flinkdemo\\src\\main\\resources\\sensors";
        DataStream<String> inputdataStream = env.readTextFile(inputPath);
        // 基于数据流进行转换计算
        DataStream<SensorReading> dataStream =
                inputdataStream.map(new MapFunction<String, SensorReading>() {
                    @Override
                    public SensorReading map(String s) throws Exception {
                        String[] fields = s.split(",");
                        return new SensorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
                    }
                });

        //分组
        KeyedStream<SensorReading, Tuple> keybyStream = dataStream.keyBy("id");

        //reduce聚合，取最大的温度值以及当前最新的时间戳
        SingleOutputStreamOperator<SensorReading> reduceStream = keybyStream.reduce(new ReduceFunction<SensorReading>() {
            @Override
            public SensorReading reduce(SensorReading t1, SensorReading t2) throws Exception {
                //做归约的时候前后两个值应该怎么计算
                //t2是当前最新的数据，t1是上一次归约的值

                return new SensorReading(t1.getId(), t2.getTimestamp(), Math.max(t1.getSensors(), t2.getSensors()));
            }
        });


        reduceStream.print();

        env.execute();
    }
}
